package library;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.HashMap;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.Timer;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumnModel;
import library.model.BooksAuthorPojo;
import library.model.DownloadFilePojo;
import library.utils.FileDownloader;
import library.utils.Utils;

/**
 * @author Dmitry
 */
public class DownloadPanel extends JPanel {
    private JTable table;
    private DownloadFilesTableModel tableModel;
    private HashMap<DownloadFilePojo, FileDownloader>downloaders=new HashMap<>();

    public DownloadPanel() {
        setLayout(new BorderLayout());
        setBorder(BorderFactory.createTitledBorder("Закачиваемые книги"));
        tableModel = new DownloadFilesTableModel();
        table = new JTable(tableModel);
        JScrollPane scroll = new JScrollPane(table);
        
        add(scroll);
        TableColumnModel cm = table.getColumnModel();
        cm.getColumn(2).setMaxWidth(30);
        cm.getColumn(1).setMaxWidth(80);
        cm.getColumn(1).setCellRenderer(new DownloadPanelCellRendererProgressColumn());
        cm.getColumn(2).setCellRenderer(new DownloadPanelCellRendererDeleteColumn());
        table.addMouseMotionListener(new MouseAdapter() {
            @Override
            public void mouseMoved(MouseEvent e) {
                int row = table.rowAtPoint(e.getPoint());
                int column = table.columnAtPoint(e.getPoint());
                if (row == -1 || column == -1) {
                    return;
                }
                if (column == 2) {
                    Utils.handCursor(table);
                } else {
                    Utils.defaultCursor(table);
                }
            }
        });
        table.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                int row = table.rowAtPoint(e.getPoint());
                int column = table.columnAtPoint(e.getPoint());
                if (row == -1 || column == -1) {
                    return;
                }
                if (column == 2) {
                    DownloadFilePojo file = tableModel.getDownloadFile(row);
                    deleteFile(file);
                }
            }
        });
        initRepaintTimer();
    }

    private void initRepaintTimer() {
        Timer timer = new Timer(100, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                table.repaint();
            }
        });
        timer.setRepeats(true);
        timer.start();
    }

    public void deleteFile(DownloadFilePojo file) {
        tableModel.delete(file);
        if(downloaders.containsKey(file)){
            FileDownloader downloader=downloaders.get(file);
            downloader.interrupt();
        }
    }

    public void download(BooksAuthorPojo toBeDownload) {
        DownloadFilePojo pojo = new DownloadFilePojo(toBeDownload.getBookName());
        pojo.setId(toBeDownload.getId());
        pojo.setAuthor(toBeDownload.getAuthor());
        tableModel.add(pojo);
        FileDownloader downloader=new FileDownloader(pojo);
        downloader.start();
        downloaders.put(pojo, downloader);
    }

    private class DownloadPanelCellRendererProgressColumn extends JProgressBar implements TableCellRenderer {
        public DownloadPanelCellRendererProgressColumn() {
            setMinimum(0);
            setMaximum(100);
        }

        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
            DownloadFilePojo df=tableModel.getDownloadFile(row);
            setValue(df.getStatus());
            if(df.isError()){
                setForeground(Color.red);
                setValue(100);
                //     setBackground(Color.RED);
            }else{
                setForeground(Color.GREEN);
            //    setBackground(Color.WHITE);
            }
            setStringPainted(true);
            setString(df.getMessage()!=null?df.getMessage():"Идет Закачка");
            
            return this;
        }
    }

    private class DownloadPanelCellRendererDeleteColumn extends JLabel implements TableCellRenderer {
        public DownloadPanelCellRendererDeleteColumn() {
            ImageIcon deleteIcon = Utils.loadIcon("stopDownload.png");
            setIcon(deleteIcon);
        }

        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
            return this;
        }
    }
}
